/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TaxaServico.java
 *
 * Created on 23/01/2011, 00:02:48
 */

package br.folhapgto.app;
import br.folhapgto.dao.EmpregadoDAO;
import br.folhapgto.app.tablemodel.EmpregadoTableModel;
import br.folhapgto.dao.TaxaServicoDAO;
import br.folhapgto.model.TaxaServico;

import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;

public class TaxaServicoForm extends javax.swing.JInternalFrame {

    /** Creates new form TaxaServico */
    public TaxaServicoForm() {
        initComponents();
        
        popularTabelaEmpregados();
        lstMesReferencia.setSelectedIndex(0);

        Calendar ano = new GregorianCalendar();
        Integer anoRef = ano.get(Calendar.YEAR);
        txtAnoReferencia.setText(anoRef.toString());
    }

    private void popularTabelaEmpregados() {
        EmpregadoDAO empregados = new EmpregadoDAO();
        tbEmpregados.setModel(new EmpregadoTableModel(empregados.lerTodos()));
    }

    private Boolean verificaCampos()
    {
        if (tbEmpregados.getRowCount() > 0)
        {
            if (txtAnoReferencia.getText().length() != 4) return false;
            if (txtTaxa.getText().equals("")) return false;
        }
        else return false;

        return true;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbEmpregados = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstMesReferencia = new javax.swing.JList();
        lblMesReferencia = new javax.swing.JLabel();
        lblTaxa = new javax.swing.JLabel();
        txtTaxa = new javax.swing.JTextField();
        btnSalvar = new javax.swing.JButton();
        lblAnoReferencia = new javax.swing.JLabel();
        txtAnoReferencia = new javax.swing.JTextField();

        setTitle("Taxa de Serviço");

        tbEmpregados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nome", "Tipo Contrato"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tbEmpregados);

        lstMesReferencia.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstMesReferencia.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(lstMesReferencia);

        lblMesReferencia.setText("Mês Referência");

        lblTaxa.setText("Taxa");

        txtTaxa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTaxaActionPerformed(evt);
            }
        });

        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        lblAnoReferencia.setText("Ano Referência");

        txtAnoReferencia.setColumns(4);
        txtAnoReferencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAnoReferenciaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblMesReferencia)
                                .addGap(58, 58, 58)
                                .addComponent(lblAnoReferencia))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtTaxa, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
                                    .addComponent(lblTaxa)
                                    .addComponent(txtAnoReferencia))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSalvar)
                        .addGap(81, 81, 81))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblMesReferencia)
                            .addComponent(lblAnoReferencia))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtAnoReferencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(7, 7, 7)
                                .addComponent(lblTaxa)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtTaxa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE))
                        .addGap(30, 30, 30)
                        .addComponent(btnSalvar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtTaxaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTaxaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTaxaActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed

        if (verificaCampos())
        {
            try
            {
                int retornoBanco = 0;
                TaxaServicoDAO taxaServDAO = new TaxaServicoDAO();
                retornoBanco = taxaServDAO.gravar(geraTaxaServico());

                if (retornoBanco>0)
                {
                    JOptionPane.showMessageDialog(null,
                                         "Taxa de serviço gravada com sucesso ",
                                         "Taxa de serviço gravada",
                                         JOptionPane.INFORMATION_MESSAGE);
                }
                else
                {
                    throw new Exception("Ocorreu uma falha ao gravar taxa de serviço");
                }
            }
            catch (Exception ex)
            {
                JOptionPane.showMessageDialog(null,
                                         ex.getMessage(),
                                         "Falha ao gravar taxa de serviço",
                                         JOptionPane.ERROR_MESSAGE);
            }
        } else
        {
            JOptionPane.showMessageDialog(null,
                                         "Verifique os campos cadastrados",
                                         "Falha ao gravar taxa de serviço",
                                         JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_btnSalvarActionPerformed

    private TaxaServico geraTaxaServico()
    {
        TaxaServico taxaServico = new TaxaServico();

        // Obtém ID empregado
        int linhaSelecionada = tbEmpregados.getSelectedRow();
        Object objIdEmpregado = tbEmpregados.getModel().getValueAt(linhaSelecionada, 0);
        int idEmpregado = Integer.parseInt(objIdEmpregado.toString());

        // Obtém mês referência
        int mesRef = lstMesReferencia.getSelectedIndex() + 1;

        // Monta objTaxaServico
        taxaServico.setIdEmpregado(idEmpregado);
        taxaServico.setMesReferencia(mesRef);
        taxaServico.setAnoReferencia(Integer.parseInt(txtAnoReferencia.getText()));
        taxaServico.setTaxaServico(Float.parseFloat(txtTaxa.getText()));

        return taxaServico;
    }
    private void txtAnoReferenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAnoReferenciaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtAnoReferenciaActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalvar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAnoReferencia;
    private javax.swing.JLabel lblMesReferencia;
    private javax.swing.JLabel lblTaxa;
    private javax.swing.JList lstMesReferencia;
    private javax.swing.JTable tbEmpregados;
    private javax.swing.JTextField txtAnoReferencia;
    private javax.swing.JTextField txtTaxa;
    // End of variables declaration//GEN-END:variables

}
